package timingAttack;

import static org.junit.Assert.*;

import java.math.BigInteger;
import java.security.SecureRandom;

import org.junit.Assert;
import org.junit.Test;

/***
 * Tests cases for ModExp class
 * 
 * @author Vincent
 *
 */
public class ModExpTest 
{

	/***
	 * Make sure the constructor that takes a BigInteger exponent
	 * produces the same answer that BigInterger's modPow produces
	 */
	@Test
	public void testModExpBigIntegerBigIntegerBigIntegerBigInteger() 
	{
		SecureRandom r = new SecureRandom();
		
		for( int i = 0; i < 100; i++ )
		{
			BigInteger mod = new BigInteger(1024,r);
			BigInteger exp = new BigInteger(1024,r);
			BigInteger base = new BigInteger(1024,r);
			
			BigInteger correctResult = base.modPow(exp, mod);
			
			ModExp result = new ModExp(BigInteger.ONE, mod, base, exp);
			
			assertEquals(0, correctResult.compareTo(result.value));
		}
	}

	/***
	 * Make sure the static modular exponentiation routine
	 * produces the same answer that BigInterger's modPow produces
	 */
	@Test
	public void testModExpBigIntegerBigIntegerBigInteger() 
	{
		SecureRandom r = new SecureRandom();
		
		for( int i = 0; i < 100; i++ )
		{
			BigInteger mod = new BigInteger(1024,r);
			BigInteger exp = new BigInteger(1024,r);
			BigInteger base = new BigInteger(1024,r);
			
			BigInteger correctResult = base.modPow(exp, mod);
			
			BigInteger result = ModExp.modExp(base, exp, mod);
			
			assertEquals(0, correctResult.compareTo(result));
		}
	}

}
